﻿#include "step.h"

Step::Step(QObject *parent)
    : QObject{parent}
    , m_strID("-1")
    , m_pParent(parent)
    , m_isRunning(false)
{}

QList<Operation *> Step::pOperations() const
{
    return m_pOperations;
}

void Step::setPOperations(const QList<Operation *> &newPOperations)
{
    m_pOperations = newPOperations;
}

QString Step::strID() const
{
    return m_strID;
}

void Step::setStrID(const QString &newStrID)
{
    m_strID = newStrID;
}

bool Step::writeXML(QXmlStreamWriter& xmlWriter){
    // 写入当前操作的信息
    xmlWriter.writeStartElement("Step");
    xmlWriter.writeAttribute(QStringLiteral("id"),m_strID);

    foreach(Operation* operation,m_pOperations){
        operation->writeXML(xmlWriter);
    }
    xmlWriter.writeEndElement();  // 结束当前操作的元素
    return true;
}

void Step::readFromXML(QXmlStreamReader& xmlReader){
    QXmlStreamAttributes attributes = xmlReader.attributes();
    if(attributes.hasAttribute("id")) {
        m_strID = attributes.value("id").toString();
    }

    while (!(xmlReader.tokenType() == QXmlStreamReader::EndElement
             && xmlReader.name() == QStringLiteral("Step"))) {

        xmlReader.readNext();
        if(xmlReader.isStartElement()){
            QString className = xmlReader.name().toString();
            Operation* pOperation = (Operation*)ObjectFactory::createObject(className.toLatin1(),m_pParent);
            if(pOperation != NULL){
                pOperation->readFromXML(xmlReader);
                this->AddOperation(pOperation);
            }
        }
    }
}


void Step::run(){
    m_isRunning = true;

    foreach(Operation* operation, m_pOperations){
        qDebug() << "ID : " << m_strID;
        qDebug() << "Operation,Classname : " << operation->getClassName();
        if(!m_isRunning)
            break;
        operation->run();
    }
}

void Step::Stop(){
    m_isRunning = false;
}
